package com.longdaji.decoration.ui.activitiesOfMine.receivingAddress;


import android.text.TextUtils;
import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.MyCities;
import com.longdaji.decoration.bean.MyProvinces;
import com.longdaji.decoration.bean.MyUniversities;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/1/21 0021.
 */

public class ReceivingAddressPresenter extends RxPresenter<ReceivingAddressContract.View> implements ReceivingAddressContract.Presenter {
    private DataManager mDataManager;
    private List<Province> mSystemProvinceList = new ArrayList<>();
    private List<City> mSystemCityList = new ArrayList<>();
    private List<County> mSystemCountyList = new ArrayList<>();
    private static final String TEST_USER_ID = "352a01f9a2244c55b4dbf0255bf892db";

    @Inject
    public ReceivingAddressPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }

    @Override
    public void onFinishClick(String userid, String contactPerson, String contactTelephone,
                              String province, String city, String district, String street,
                              String detailAddress, String createTime, String defaultAddress,
                              int shippingId, String zipcode, String delFlag) {
        if (shippingId == -1) {
            addSubscribe(mDataManager.doAddReceivingAddressApi(userid, contactPerson, contactTelephone,
                    province, city, district, street, detailAddress,
                    createTime, defaultAddress, zipcode, delFlag)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new CommonSubscriber<HttpResponse>() {
                        @Override
                        public void onSuccess(HttpResponse httpResponse) {
                            Log.d("ReceivingAddress", "succeed : doAddReceivingAddressApi ");
                            if (TextUtils.equals("0", httpResponse.getStatusCode())) {
                                mView.showResult(true);
                            } else {
                                mView.showResult(false);
                            }

                        }

                        @Override
                        public void onFailed(String msg) {
                            Log.d("ReceivingAddress", "failed : " + msg);
                            mView.showResult(false);
                        }
                    }));
        } else {
            addSubscribe(mDataManager.doUpdateReceivingAddressApi(userid, contactPerson, contactTelephone,
                    province, city, district, street, detailAddress,
                    createTime, defaultAddress, shippingId, zipcode, delFlag)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new CommonSubscriber<HttpResponse>() {
                        @Override
                        public void onSuccess(HttpResponse httpResponse) {
                            Log.d("ReceivingAddress", "succeed : doAddReceivingAddressApi ");
                            if (TextUtils.equals("0", httpResponse.getStatusCode())) {
                                mView.showUpdateAddrSuccess();
                            } else {
                                mView.showUpdateAddrFail();
                            }

                        }

                        @Override
                        public void onFailed(String msg) {
                            Log.d("ReceivingAddress", "failed : " + msg);
                            mView.showUpdateAddrFail();
                        }
                    }));
        }


    }

    @Override
    public List<Province> getProvince() {
        return mSystemProvinceList;
    }

    @Override
    public List<City> getCity() {
        return mSystemCityList;
    }

    @Override
    public List<County> getCampus() {
        return mSystemCountyList;
    }

    @Override
    public void initCampusPicker() {
        addSubscribe(mDataManager.doGetProvince()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<MyProvinces>handleResult())
                .subscribeWith(new CommonSubscriber<MyProvinces>() {
                    @Override
                    public void onSuccess(MyProvinces myProvinces) {
                        Log.d("mytest", "provinces : " + myProvinces.getProvinces().size());
//                        mMyProvinceList = myProvinces.getProvinces();
                        for (MyProvinces.ProvincesBean province : myProvinces.getProvinces()) {
                            Province systemProvince = new Province();
                            systemProvince.id = province.getId();
                            systemProvince.name = province.getName();
                            mSystemProvinceList.add(systemProvince);
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);
                    }
                }));

        // TODO: 2018/3/17 0017 市区和学校List
        addSubscribe(mDataManager.doGetCity()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<MyCities>handleResult())
                .subscribeWith(new CommonSubscriber<MyCities>() {
                    @Override
                    public void onSuccess(MyCities myCities) {
                        Log.d("mytest", "cities : " + myCities.getAllCity().size());
//                        mMyCityList = myCities.getAllCity();
                        for (MyCities.AllCityBean city : myCities.getAllCity()) {
                            City systemCity = new City();
                            systemCity.id = city.getId();
                            systemCity.name = city.getName();
                            systemCity.province_id = city.getProvincesId();
                            mSystemCityList.add(systemCity);
                        }

                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d("mytest", "failed : " + msg);

                    }
                }));

        addSubscribe(mDataManager.doGetCampus()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<MyUniversities>handleResult())
                .subscribeWith(new CommonSubscriber<MyUniversities>() {
                    @Override
                    public void onSuccess(MyUniversities myUniversities) {
                        Log.d("mytest", "universities : " + myUniversities.getAllUniversity().size());
//                        mMyUniversityList = myUniversities.getAllUniversity();
                        for (MyUniversities.AllUniversityBean university : myUniversities.getAllUniversity()) {
                            County systemCounty = new County();
                            systemCounty.id = university.getId();
                            systemCounty.name = university.getName();
                            systemCounty.city_id = university.getCityId();
                            mSystemCountyList.add(systemCounty);
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d("mytest", "failed : " + msg);
                    }
                }));
    }

}
